/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.apt.adminlog;

import com.apt.bean.User;
import com.apt.tag.Connect_DB;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 *
 * @author Bach
 */
public class AdminLogin {

    public static User admin_Login(String us, String pass,String sql) {
        User user = null;
        Connection cnn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
//        String sql = "select * from Accounts where Account_Status=1 and Account_Role='admin' and User_Name=? and User_Password=? ";
        try {

            cnn = Connect_DB.getConnection();
            ps = cnn.prepareStatement(sql);
            ps.setString(1, us);
            ps.setString(2, pass);
            rs = ps.executeQuery();
            while (rs.next()) {
                int Account_Id = rs.getInt("Account_Id");
                String User_Password = rs.getString("User_Password");
                String User_Name = rs.getString("User_Name");
                String User_Email = rs.getString("User_Email");
                byte Account_Status = rs.getByte("Account_Status");
                String Date_Registed = rs.getString("Date_Registed");
                String Date_Expired = rs.getString("Date_Expired");
                String Account_Rol = rs.getString("Account_Role");
                user = new User(Account_Id, User_Password, User_Name, User_Email, Account_Status, Date_Registed, Date_Expired, Account_Rol);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return user;
    }

    public static User admin_Login(int id) {
        User user = null;
        Connection cnn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = "select * from Accounts where Account_Status=1 and Account_Role='admin' and [Account_Id]=?";
        try {

            cnn = Connect_DB.getConnection();
            ps = cnn.prepareStatement(sql);
            ps.setInt(1, id);
            rs = ps.executeQuery();
            while (rs.next()) {
                int Account_Id = rs.getInt("Account_Id");
                String User_Password = rs.getString("User_Password");
                String User_Name = rs.getString("User_Name");
                String User_Email = rs.getString("User_Email");
                byte Account_Status = rs.getByte("Account_Status");
                String Date_Registed = rs.getString("Date_Registed");
                String Date_Expired = rs.getString("Date_Expired");
                String Account_Rol = rs.getString("Account_Role");
                user = new User(Account_Id, User_Password, User_Name, User_Email, Account_Status, Date_Registed, Date_Expired, Account_Rol);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return user;
    }
}
